//
//  StoreEdit.swift
//  Share
//
//  Created by admin on 2020/10/10.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI

struct StoreEdit: View {
    
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var pageStore:StoreEditStore
    
    var body: some View {
        NavigationView{
        ScrollView{
            
            Section() {
                
                VStack(alignment: .leading){
                    Text("买入数量")
                    TextField("买入数量",text:$pageStore.num)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                
                VStack(alignment: .leading){
                    Text("输入价格")
                    TextField("输入价格",text:$pageStore.price)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                VStack(alignment: .leading){
                    Text("买入时间")
                    TextField("输入时间 2020-01-01",text:$pageStore.day)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                
            }
            
            Section() {
                
                
                VStack(alignment: .leading){
                    Text("是否卖出")
                    TextField("是否卖出：",text:$pageStore.state)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                VStack(alignment: .leading){
                    Text("卖出价格")
                    TextField("卖出价格 2020-01-01",text:$pageStore.outprice)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                VStack(alignment: .leading){
                    Text("买出时间")
                    TextField("2020-01-01",text:$pageStore.outday)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
                VStack(alignment: .leading){
                    Text("手续费(元)")
                    TextField("0.00",text:$pageStore.fee)
                        .padding(.leading)
                        .frame(height: 44)
                        .border(Color.grey_3.opacity(0.3), width: 0.5)
                }
            }
        }
        .foregroundColor(.grey_2)
        .font(.system(size: 12))
        .padding(.all)
        
        .onAppear{
            pageStore.update()
        }
        .onDisappear(perform: {
     
        })
        .alert(item: $pageStore.tostError){tostError in
            Alert(
                title: Text(verbatim: tostError.title),
                message: Text(tostError.msg)
            )
        }
      
        .navigationBarTitle("新增/修改",displayMode: .inline)
        .navigationBarItems(
            trailing:HStack{
                Button(action:save) {
                    Text("保存")
                        .foregroundColor(.grey_1)
            
                }
                Button(action:delete ) {
                    Text("删除")
                        .foregroundColor(.grey_1)
                
                }
            }.font(.system(size: 12))
        )
        }
    }
    func save() {
        pageStore.save { (error) in
            if error == nil{
                presentationMode.wrappedValue.dismiss()
            }else{
                pageStore.tostError = TostError.init(code: -1, msg: "保存失败", title: "警告", level: .warm)
            }
        }
        
    }
    
    func delete() {
        do {
           try pageStore.delete { (error) in
                if error == nil{
                    presentationMode.wrappedValue.dismiss()
                }else{
                    pageStore.tostError = TostError.init(code: -1, msg: "删除失败", title: "警告", level: .warm)
                }
            }
        } catch  {
            pageStore.tostError = error as? TostError
        }
        
    }
    
}

struct StoreEdit_Previews: PreviewProvider {
    static var previews: some View {
        StoreEdit(pageStore: StoreEditStore(id: 0,shareId: 0))
        
    }
}


